﻿/*******************************************************************************
*Author         :  VN
*Description    :  发包缓存结构体
*Other          :  none
*Modify Record  :  2015/7/6
*******************************************************************************/

using System.Collections.Generic;
using System.Collections;
using System.Text;
using UnityEngine;

namespace Network
{
    public class MsgInfo
    {
        private int   m_AutoReTryTimes;
        public int    AutoReTryTimes
        {
            get { return m_AutoReTryTimes; }
        }
        uint m_uSeqId;
        public uint uSeqId
        {
            get { return m_uSeqId; }
            set { m_uSeqId = value; }
        }

        uint m_CmdId;
        public uint CmdId
        {
            get { return m_CmdId; }
        }

        float m_Timeout;
        public float Timeout
        {
            get { return m_Timeout; }
        }

        bool m_bNeedRes;
        public bool NeedRes
        {
            get { return m_bNeedRes; }
        }

        byte[]  m_Msg;
        public byte[] Msg
        {
            get { return m_Msg; }
        }

        CMsgDelegate m_CallBack;
        public CMsgDelegate Callback
        {
            get { return m_CallBack; }
        }

        private float m_SentTime;
/*        private LuaState luaState;*/

        public MsgInfo(uint uSeqId, uint cmdID, byte[] msg, CMsgDelegate callback, int retrytimes, float fTimeout, bool bNeedRes)
        {
            Set(uSeqId, cmdID, msg, callback, retrytimes, fTimeout, bNeedRes);
        }

        public void Set(uint uSeqId, uint cmdID, byte[] msg, CMsgDelegate callback, int retrytimes, float fTimeout, bool bNeedRes)
        {
            m_uSeqId = uSeqId;
            m_CmdId = cmdID;
            m_Msg = msg;
            m_CallBack = callback;
            m_Timeout = fTimeout;
            m_SentTime = Time.realtimeSinceStartup;
            if (m_Timeout < 1.0f)
            {
                m_Timeout = 5.0f;
            }
            m_bNeedRes = bNeedRes;
            m_AutoReTryTimes = retrytimes;
/*            luaState = LuaClient.GetMainState();*/
        }

        public void Reset()
        {
            m_uSeqId = 0;
            m_CmdId = 0;
            m_Msg = null;
            m_CallBack = null;
            m_bNeedRes = false;
			m_AutoReTryTimes = 0;
            m_Timeout = 0;
/*            luaState = null;*/
        }

        public bool Send(CmdPack pack, PackCodec codec, TCPChnl tcpChnl)
        {
            m_SentTime = Time.realtimeSinceStartup;
            pack.Set(CmdId, uSeqId, Msg);   // 打包头
            ByteBuffer bufData = codec.PackData(pack);
            m_AutoReTryTimes = m_AutoReTryTimes - 1;
            return tcpChnl.SendData(bufData.ToBytes(), bufData.ToBytes().Length);
        }

        public bool Resend(CmdPack pack, PackCodec codec, TCPChnl tcpChnl)
        {
            if (m_AutoReTryTimes >= 0)
            m_SentTime = Time.realtimeSinceStartup;
            pack.Set(CmdId, uSeqId, Msg);   // 打包头
            ByteBuffer bufData = codec.PackData(pack);
            m_AutoReTryTimes = m_AutoReTryTimes - 1;
            return tcpChnl.SendData(bufData.ToBytes(), bufData.ToBytes().Length);
        }

        public void HandleCallBack(byte[] RecvMsg)
        {
            if (Callback != null)
            {
                Callback(CmdId, CMsgResult.NoError, RecvMsg, Msg);
            }

//             if (luaState != null)  // lua收包回调
//             {
//                 LuaFunction func = luaState.GetFunction("NetworkMgr.OnSocket");
//                 func.BeginPCall();
//                 func.Push(CmdId);
//                 func.Push(uSeqId);
//                 func.PushByteBuffer(RecvMsg);
//                 func.PCall();
//                 func.EndPCall();
//                 func.Dispose();
//                 func = null;
//             }
        }

        public void HandleTimeout(CMsgResult reason)
        {
            if (Callback != null)
            {
                Callback(CmdId, reason, null, Msg);
            }

//             if (luaState != null)  // lua收包回调
//             {
//                 LuaFunction func = luaState.GetFunction("NetworkMgr.OnSocketTimeout");
//                 func.BeginPCall();
//                 func.Push(CmdId);
//                 func.Push(uSeqId);
//                 func.PushByteBuffer(Msg);
//                 func.Push((int)reason);
//                 func.PCall();
//                 func.EndPCall();
//                 func.Dispose();
//                 func = null;
//             }
        }

        //检测是否超时
        public bool IsTimeOut(float CurTime)
        {
            return (m_SentTime + m_Timeout) < CurTime;
        }
    }
}